import numpy as np
# 对比函数

# greater(array, [数字，/array2]   是数字则，array中的每个元素与数字相比较，如果是数组，则相对应位置作比较，是否大于
r = np.array([1,2,3,4,5,6,7,8,9,10,11,12])
print(np.greater(r,4))
# print(np.greater(r,[10,11,2,3,4,13,4,54,454,4,4])) #报错，因为作比较的第二个数组与第一个数组形状不相同
print(np.greater_equal(r,4),'==大于等于')

# 是否小于
print(np.less(r,4),'--less')
print(np.less(r,[1,2,3,4,5,6,7,8,9,20,11,12]),'==less')
print(np.less_equal(r,[1,1,3,4,5,6,7,8,9,20,11,12]),'++less 小于等于')

# 是否等于,
print(np.equal(r,11))
print(np.equal(r,[1,2,3,4,5,6,7,8,9,10,11,12]),'==equal')
# 是否不等于
print(np.not_equal(r,[1,2,3,4,5,6,7,8,9,10,11,12]),'!=equal')

# 逻辑运算
print(np.logical_and([1,0,1],[0,1,1]),'===逻辑与')
print(np.logical_not([1,0,1]),'逻辑非')
print(np.logical_or([1,0,1],[0,1,1]),'==<逻辑或>')
print(np.logical_xor([1,0,1],[0,1,1]),'==<逻辑异或>')

# 检查是否有一个元素为True，有则返回True 否则返回False
print(np.any([1,0,1],),'==<any>')
print(np.all([0,0],),'==<any>')
# 检查是否全为True
print(np.all([1,0,1]),'==all')
print(np.all([1,1,1]),'==all')

# 条件筛选
# np.where(条件，成功取值，失败值为)
arr = np.array([1,2,3,4,5,6,7,8,9,10,11,12])
print(np.where(arr > 3, arr,0))
print(np.where(arr < 5, arr,'失败'))
# 嵌套写法
print(np.where(arr<3,arr,np.where(arr<5,arr,'b')))



arr2 = np.random.randint(50,100,20)
print(arr2)
# 注意select必须有default
print(np.select([arr2>80,(arr2>=60)&(arr2<=80), arr2<60],['优','良','不及格'],default="未知"))
